package com.winit.openapi.oauth.manager.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.winit.common.spi.command.GetByKeyCommand;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiBizException;
import com.winit.openapi.oauth.dao.APIDeveloperDao;
import com.winit.openapi.oauth.entity.APIDeveloperEntity;
import com.winit.openapi.oauth.manager.APIDeveloperManager;
import com.winit.openapi.oauth.util.OAuth2SecurityUtil;
import com.winit.openapi.oauth.vo.APIDeveloperVo;
import com.winit.openapi.util.BeanUtils;
import com.winit.ums.spi.UmsUserService;
import com.winit.ums.spi.vo.user.UserVO;

@Service("apiDeveloperManager")
public class APIDeveloperManagerImpl implements APIDeveloperManager {

    private Logger          logger = LoggerFactory.getLogger(APIDeveloperManagerImpl.class);

    @Resource
    private APIDeveloperDao apiDeveloperDao;

    @Resource
    private UmsUserService  umsUserService;

    @Override
    @Transactional
    public int insertDeveloper(APIDeveloperVo vo) {

        try {
            APIDeveloperEntity entity = BeanUtils.copyProperties(vo, APIDeveloperEntity.class);
            if (entity == null) {
                return -1;
            }
            return apiDeveloperDao.insertDeveloper(entity);
        } catch (Exception e) {
            logger.error("开发者注册异常,error:{}", e);
            throw new ApiBizException(ErrorCode.OAUTH_DEVELOPER_CREATE_ERROR);

        }

    }

    @Override
    public APIDeveloperVo queryById(Long developerId) {

        try {
            APIDeveloperEntity entity = apiDeveloperDao.selectById(developerId);
            if (null == entity) {
                return null;
            }
            return BeanUtils.copyProperties(entity, APIDeveloperVo.class);
        } catch (Exception e) {
            logger.error("开发者查询异常,error:{}", e);
            throw new ApiBizException(ErrorCode.OAUTH_DEVELOPER_QUERY_ERROR);
        }

    }

    @Override
    public boolean checkExists(String developerAccount) {
        APIDeveloperEntity entity = apiDeveloperDao.selectByAccount(developerAccount);

        return entity != null;
    }

    @Override
    public void updateDeveloper(APIDeveloperVo vo) {
        try {
            APIDeveloperEntity entity = BeanUtils.copyProperties(vo, APIDeveloperEntity.class);
            if (entity == null) {
                return;
            }

            apiDeveloperDao.updateDeveloper(entity);
        } catch (Exception e) {
            logger.error("更新开发者信息异常,error:{}", e);
            throw new ApiBizException(ErrorCode.OAUTH_DEVELOPER_UPDATE_ERROR);
        }

    }

    @Override
    public APIDeveloperVo login(String account, String password) {

        String passwordEncrypted = OAuth2SecurityUtil.md5AndSha256(password, OAuth2SecurityUtil.md5(password));
        APIDeveloperEntity loginEntity = apiDeveloperDao.login(account, passwordEncrypted);

        return BeanUtils.copyProperties(loginEntity, APIDeveloperVo.class);
    }

    @Override
    public APIDeveloperVo loginByWinitAccount(Long winitUserId) {

        APIDeveloperEntity loginEntity = apiDeveloperDao.loginByWinitAccount(winitUserId);

        return BeanUtils.copyProperties(loginEntity, APIDeveloperVo.class);
    }

    @Override
    public APIDeveloperVo selectByAccount(String developerAccount) {
        try {
            APIDeveloperEntity entity = apiDeveloperDao.selectByAccount(developerAccount);
            if (null == entity) {
                // 如果查询为空，则查询WINIT账号关联的开发者账号
                GetByKeyCommand<String> getByKeyCommand = new GetByKeyCommand<String>();
                getByKeyCommand.setSearchKey(developerAccount);
                UserVO userVo = umsUserService.queryByUsername(getByKeyCommand);
                if (userVo == null) {
                    return null;
                }
                return loginByWinitAccount(userVo.getId());
            }
            return BeanUtils.copyProperties(entity, APIDeveloperVo.class);
        } catch (Exception e) {
            logger.error("开发者查询异常,error:{}", e);
            throw new ApiBizException(ErrorCode.OAUTH_DEVELOPER_QUERY_ERROR);
        }
    }

    @Override
    public APIDeveloperVo find(APIDeveloperVo vo) {
        if (vo == null) {
            return null;
        }
        try {
            APIDeveloperEntity entity = BeanUtils.copyProperties(vo, APIDeveloperEntity.class);

            APIDeveloperEntity result = apiDeveloperDao.find(entity);

            if (result == null) {
                return null;
            }

            return BeanUtils.copyProperties(result, APIDeveloperVo.class);
        } catch (Exception e) {
            logger.error("开发者查询异常,error:{}", e);
            throw new ApiBizException(ErrorCode.OAUTH_DEVELOPER_QUERY_ERROR);
        }
    }
}
